package cn.liyicun.cp.cpweb.controller;

import cn.liyicun.cp.cpweb.entity.LoginInfo;
import cn.liyicun.cp.cpweb.entity.UserMoney;
import cn.liyicun.cp.cpweb.service.UserInfoService;
import cn.liyicun.cp.cpweb.service.UserMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Controller
public class IndexController extends BaseController {


    @Autowired
    UserInfoService userInfoService;

    @Autowired
    private UserMoneyService userMoneyService;

    @RequestMapping(value = "/")
    public String index(HttpServletRequest request, Map<String, Object> map) {
        map.put("title", "CP系统");
        map.put("now", new Date());
        // 跳转到 templates 目录下的 uploadimg.html

        LoginInfo info = getLoginInfo(request, false);


        if (info != null) {
            UserMoney moneyInfo = userMoneyService.getMoneyInfo(info.getUid());
            if (moneyInfo != null) {
                if(info.getMoney().compareTo(moneyInfo.getMoney())!=0) {
                    info.setMoney(moneyInfo.getMoney());
                    setLoginInfo(request, info);
                }
            }

        }


        return "index";
    }
}
